var app = getApp()

Page({
  data: {
    data: null,
    postId: '',
    hide: true,
    content: "",
    comments: [],
    commentCount: 0,
    likeurl: "../image/like.png",
    likeCount: 0
  }, onLoad: function (options) {
    wx.setNavigationBarTitle({ title: "动态" })
    this.setData({ postId: options.postId })
    wx.cloud.callFunction({ name: 'function', data: { type: "get", table: "post", where: { _id: options.postId } } }).then(res => {
      this.setData({ data: res.result.user.data[0] })
      if (res.result.user.data[0].comments) {
        this.setData({ commentCount: res.result.user.data[0].comments })
      } else {
        this.setData({ commentCount: 0 })
      }
      if (res.result.user.data[0].comments) {
        this.setData({ likeCount: res.result.user.data[0].like })
      } else {
        this.setData({ likeCount: 0 })
      }
    })
    wx.cloud.callFunction({ name: 'function', data: { type: "get", table: "comment", where: { postId: options.postId } } }).then(res => {
      this.setData({ comments: res.result.user.data })
      //增加浏览量
      wx.cloud.callFunction({ name: 'function', data: { type: "increaseLook", where: { _id: options.postId } } })
    })
  }, onShareAppMessage() {
    return {
      title: this.data.data.userInfo.nickName + '的动态',
      desc: this.data.data.content,
      path: './postView?postId=' + this.data.data._id
    }
  }, addComment() {
    if (!(app.globalData.hasUserInfo && app.globalData.database != null)) {
      wx.showToast({ title: '请登录并完善个人信息', icon: 'none', duration: 1000, success: function () { setTimeout(function () { wx.reLaunch({ url: '/pages/me/me', }) }, 1000) } })
    } else {
      this.setData({ hide: false, content: "" })
    }
  }, inputToastBinder(a) {
    this.setData({
      content: a.detail.value
    })
  }, inputToastCancel() {
    this.setData({
      hide: true,
      content: ""
    })
  }, inputToastConfirm() {
    this.setData({ hide: true })
    var time = Date.parse(new Date())
    var date = new Date(parseInt(time))
    var d = { content: this.data.content, postId: this.data.postId, commentator: app.globalData.userInfo, user_id: app.globalData.database._id, updateTime: time / 1000, updateTimeDes: date.getFullYear() + '/' + (date.getMonth() + 1) + "/" + date.getDate() + " " + date.getHours() + ":" + date.getMinutes() }
    wx.cloud.callFunction({ name: 'function', data: { type: "addComment", data: d } }).then(res => {
      d._id = res.result.user._id
      this.data.comments.push(d)
      if (this.data.commentCount) {
        this.setData({ comments: this.data.comments, commentCount: this.data.commentCount + 1 })
      } else {
        this.setData({ comments: this.data.comments, commentCount: 1 })
      }
    })
  }, previewImage: function (e) {
    wx.previewImage({ urls: this.data.data.imgs, current: [e.currentTarget.dataset.url] })
  }, commentTap(a) {
    var that = this
    if (app.globalData.hasUserInfo && app.globalData.database != null) {
      /*1文章主人是我我可以删评论
      2，不是我，但评论者是我，我可以删 */
      if ((app.globalData.database._id == this.data.data.user_id) || (app.globalData.database._id == a.currentTarget.dataset.userid)) {
        var that = this
        wx.showActionSheet({
          itemList: ['删除评论'], success: function (res) {
            if (res.tapIndex == 0) {
              wx.cloud.callFunction({ name: 'function', data: { type: "deleteComment", id: a.currentTarget.dataset.commentid } })
              var d = new Array()
              for (var i = 0; i < that.data.comments.length; i++) {
                if (that.data.comments[i]._id != a.currentTarget.dataset.commentid) {//自写自删俩都是undefine即true符合两种要求
                  d.push(that.data.comments[i])
                }
              }
              that.setData({ comments: d, commentCount: that.data.commentCount - 1 })
            }
          }
        })
      }
    }
  }, addLike() {
    if (!(app.globalData.hasUserInfo && app.globalData.database != null)) {
      wx.showToast({ title: '请登录并完善个人信息', icon: 'none', duration: 1000, success: function () { setTimeout(function () { wx.reLaunch({ url: '/pages/me/me', }) }, 1000) } })
    } else {
      if (this.data.likeurl == "../image/like.png") {
        if (this.data.likeCount) {
          this.setData({ likeurl: "../image/liked.png", likeCount: this.data.likeCount + 1 })
        } else {
          this.setData({ likeurl: "../image/liked.png", likeCount: 1 })
        }
        wx.cloud.callFunction({ name: 'function', data: { type: "increaseLike", where: { _id: this.data.postId } } })
      } else {
        this.setData({ likeurl: "../image/like.png", likeCount: this.data.likeCount - 1 })
        wx.cloud.callFunction({ name: 'function', data: { type: "decreaseLike", where: { _id: this.data.postId } } })
      }
    }




  }
})